﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class UserControl_ControlRegisterNPP : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag"] == null)
            {
                ViewState["flag"] = false;
            }
           

            BindTinhThanh();


            //nằm trong thread
            ThreadStart thrstar = new ThreadStart(delegate() { BindQuanHuyen(ddlTinhThanh.SelectedValue.ToString());});
            Thread tr=new Thread(thrstar);

            ThreadStart thrstar1 = new ThreadStart(delegate() { Bind3QuanHuyen(ddlTinhThanhHoatDong.SelectedValue.ToString()); });
            Thread tr1=new Thread(thrstar1);

            ThreadStart thrstar2 = new ThreadStart(BindGender);
            Thread tr2 = new Thread(thrstar2);

            ThreadStart thrstar3 = new ThreadStart(BindEducation);
            Thread tr3 = new Thread(thrstar3);

            tr.Start();
            tr1.Start();
            tr2.Start();
            tr3.Start();


            tr.Join();
            tr1.Join();
            tr2.Join();
            tr3.Join();
           
            //nằm trong thread
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        txtMaSo.Text = "New";
        txtMaRecommender.Text = "";
        txtName.Text = "";
        txtNgayGiaNhap.Text = DateTime.Now.ToString("dd/MM/yyyy");
        txtNgaySinh.Text = "";
        txtNgheNghiep.Text = "";
        txtPhone.Text = "";
        txtSearch.Text = "";
        txtSoCMND.Text = "";
        txtSoNha.Text = "";
        txtStreet.Text = "";
        txtMaRecommender.ReadOnly = false;
        txtTenRecommender.ReadOnly = false;
        txtTenRecommender.Text = "";

        ViewState["flag"] = true;
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        string salesp_key="";
        try
        {
            if ((bool)ViewState["flag"])
            {
                //insert
                salesp_key = dt.GetDataDepend(str, "k_insertArsalesp", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@salesp_name", txtName.Text),
                    new SqlParameter("@BirthDate", txtNgaySinh.Text),
                    new SqlParameter("@Gender", ddlXungHo.SelectedValue.ToString()),
                    new SqlParameter("@Address", txtSoNha.Text),
                    new SqlParameter("@Street", txtStreet.Text),
                    new SqlParameter("@Province", ddlTinhThanh.SelectedValue.ToString()),
                    new SqlParameter("@Town", ddlQuanHuyen.SelectedValue.ToString()),
                    new SqlParameter("@Mobile", txtPhone.Text),
                    new SqlParameter("@IDCard", txtSoCMND.Text),
                    new SqlParameter("@IDWhere", ddlNoiCap.SelectedValue.ToString()),
                    new SqlParameter("@EducationLevel", ddlTrinhDo.SelectedValue.ToString()),
                    new SqlParameter("@Job", txtNgheNghiep.Text),
                    new SqlParameter("@ContractDate", txtNgayGiaNhap.Text),
                    new SqlParameter("@Active", chActive.Checked),
                    new SqlParameter("@Recommender", txtMaRecommender.Text),
                    new SqlParameter("@TinhThanhHoatDong", ddlTinhThanhHoatDong.SelectedValue.ToString()),
                    new SqlParameter("@QuanHuyen1", ddlQuanHuyen1.SelectedValue.ToString()),
                    new SqlParameter("@QuanHuyen2", ddlQuanHuyen2.SelectedValue.ToString()),
                    new SqlParameter("@QuanHuyen3", ddlQuanHuyen3.SelectedValue.ToString()),
                    new SqlParameter("@sitecode",Session["sitecode"].ToString()));
                if ((salesp_key != "not") || (salesp_key != ""))
                {
                    txtMaSo.Text = salesp_key;
                    txtMaRecommender.ReadOnly = true;
                    txtTenRecommender.ReadOnly = true;
                    ViewState["flag"] = false;
                    uscMsgBox1.AddMessage("Đăng ký nhà phân phối mới thành công,đề nghị bạn ghi nhớ lại mã", uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage(DRMConstants.MESSAGE_UNDER_CONSTRUCTION, uscMsgBox.enmMessageType.Success);
                }
            }
            else
            {
                //update
                int i=dt.ExecuteSQl_int(str,"k_updateArsalesp",CommandType.StoredProcedure,ref error,
                     new SqlParameter("@salesp_name", txtName.Text),
                    new SqlParameter("@BirthDate", txtNgaySinh.Text),
                    new SqlParameter("@Gender", ddlXungHo.SelectedValue.ToString()),
                    new SqlParameter("@Address", txtSoNha.Text),
                    new SqlParameter("@Street", txtStreet.Text),
                    new SqlParameter("@Province", ddlTinhThanh.SelectedValue.ToString()),
                    new SqlParameter("@Town", ddlQuanHuyen.SelectedValue.ToString()),
                    new SqlParameter("@Mobile", txtPhone.Text),
                    new SqlParameter("@IDCard", txtSoCMND.Text),
                    new SqlParameter("@IDWhere", ddlNoiCap.SelectedValue.ToString()),
                    new SqlParameter("@EducationLevel", ddlTrinhDo.SelectedValue.ToString()),
                    new SqlParameter("@Job", txtNgheNghiep.Text),
                    new SqlParameter("@TinhThanhHoatDong", ddlTinhThanhHoatDong.SelectedValue.ToString()),
                    new SqlParameter("@QuanHuyen1", ddlQuanHuyen1.SelectedValue.ToString()),
                    new SqlParameter("@QuanHuyen2", ddlQuanHuyen2.SelectedValue.ToString()),
                    new SqlParameter("@QuanHuyen3", ddlQuanHuyen3.SelectedValue.ToString()),
                    new SqlParameter("@salesPkey",txtMaSo.Text));
                if (i == 1)
                {
                    uscMsgBox1.AddMessage("Cập nhật thông tin nhà phân phối thành công", uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage("Đề nghị cung cấp mã nhà phân phối để cập nhật", uscMsgBox.enmMessageType.Error);
                }
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
    protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void grvNPP_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == "chon")
            {

                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                Label lblMaSo = (Label)grv.FindControl("lblMaSo");

                DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getDetailNPP", CommandType.StoredProcedure,
                    new SqlParameter("@salesp_key", lblMaSo.Text),
                    new SqlParameter("@sitecode",Session["sitecode"].ToString()));
                txtMaSo.Text = db.Rows[0]["salesp_key"].ToString();
                txtName.Text = db.Rows[0]["salesp_name"].ToString();
                txtNgaySinh.Text= db.Rows[0]["BirthDate"].ToString();
                try
                {
                    ddlXungHo.SelectedValue=db.Rows[0]["Gender"].ToString();
                }
                catch
                {
                }
                txtSoNha.Text=db.Rows[0]["Address"].ToString();
                txtStreet.Text=db.Rows[0]["Street"].ToString();
                try
                {
                    ddlTinhThanh.SelectedValue=db.Rows[0]["Province"].ToString();
                    BindQuanHuyen(ddlTinhThanh.SelectedValue.ToString());
                }
                catch
                {

                }
                try
                {
                    ddlQuanHuyen.SelectedValue=db.Rows[0]["Town"].ToString();
                }
                catch
                {
                }
                txtPhone.Text=db.Rows[0]["Mobile"].ToString();
	            txtSoCMND.Text=db.Rows[0]["IDCard"].ToString();
                try
                {
                    ddlNoiCap.SelectedValue=db.Rows[0]["IDWhere"].ToString();
                }
                catch
                {

                }
                try
                {
                    ddlTrinhDo.SelectedValue=db.Rows[0]["EducationLevel"].ToString();
                }
                catch
                {
                }
                txtNgheNghiep.Text=db.Rows[0]["Job"].ToString();
                txtNgayGiaNhap.Text=db.Rows[0]["ContractDate"].ToString();
                chActive.Checked=bool.Parse(db.Rows[0]["Active"].ToString());
                txtMaRecommender.Text=db.Rows[0]["Recommender"].ToString();
                txtTenRecommender.Text=db.Rows[0]["TenRecommender"].ToString();
                try
                {
                    ddlTinhThanhHoatDong.SelectedValue=db.Rows[0]["TinhThanhHoatDong"].ToString();
                    Bind3QuanHuyen(ddlTinhThanhHoatDong.SelectedValue.ToString());
                }
                catch
                {
                }
                try
                {
                    ddlQuanHuyen1.SelectedValue=db.Rows[0]["QuanHuyen1"].ToString();
                }
                catch
                {
                }
                try
                {
                    ddlQuanHuyen2.SelectedValue=db.Rows[0]["QuanHuyen2"].ToString();
                }
                catch
                {
                }
                try
                {
                    ddlQuanHuyen3.SelectedValue=db.Rows[0]["QuanHuyen3"].ToString();
                }
                catch
                {
                }
      
                grvNPP.DataSource = null;
                grvNPP.DataBind();

                txtMaRecommender.ReadOnly = true;
                txtTenRecommender.ReadOnly = true;

                ViewState["flag"] = false;
                TabContainer1.ActiveTabIndex = 0;

            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
    protected void btSearch_Click(object sender, EventArgs e)
    {
       
    }
    protected void ddlTinhThanhHoatDong_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind3QuanHuyen(ddlTinhThanh.SelectedValue.ToString());
    }
    protected void ddlTinhThanh_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindQuanHuyen(ddlTinhThanh.SelectedValue.ToString());
    }
    void BindTinhThanh()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getTinhThanh", CommandType.StoredProcedure);

        dt.BindDropDownList(db, ddlTinhThanh, "MaTinh", "TinhThanh");
        dt.BindDropDownList(db, ddlTinhThanhHoatDong, "MaTinh", "TinhThanh");
        dt.BindDropDownList(db, ddlNoiCap, "MaTinh", "TinhThanh");
        
    }
    void BindGender()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getListGender", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlXungHo, "GenderID", "GenderName");
    }
    void BindEducation()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getListEdu", CommandType.StoredProcedure);
        dt.BindDropDownList(db,ddlTrinhDo, "EduID", "EduName");
    }
    void BindQuanHuyen(string MaTinh)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getQuanHuyen", CommandType.StoredProcedure,
            new SqlParameter("@MaTinh", MaTinh));
        dt.BindDropDownList(db, ddlQuanHuyen, "MaQuan", "QuanHuyen");
    }
    void Bind3QuanHuyen(string MaTinh)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getQuanHuyen", CommandType.StoredProcedure,
            new SqlParameter("@MaTinh", MaTinh));
        dt.BindDropDownList(db, ddlQuanHuyen1, "MaQuan", "QuanHuyen");
        dt.BindDropDownList(db, ddlQuanHuyen2, "MaQuan", "QuanHuyen");
        dt.BindDropDownList(db, ddlQuanHuyen3, "MaQuan", "QuanHuyen");
    }
    protected void grvNPP_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvNPP.PageIndex = e.NewPageIndex;
        LoadGrid();

    }
    void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "K_FindInforNPP", CommandType.StoredProcedure,
                new SqlParameter("@parameter", txtSearch.Text),
                new SqlParameter("@sitecode",Session["sitecode"].ToString()));
            grvNPP.DataSource = db;
            grvNPP.DataBind();
            if (db.Rows.Count == 0)
            {
                uscMsgBox1.AddMessage("Không tìm thấy kết quả", uscMsgBox.enmMessageType.Attention);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void txtMaRecommender_TextChanged(object sender, EventArgs e)
    {
        try
        {
            string Chuoi = txtMaRecommender.Text;
            int index = Chuoi.IndexOf("|");
            txtMaRecommender.Text = Chuoi.Substring(0, index - 1);
            txtTenRecommender.Text = Chuoi.Substring(index + 2, Chuoi.Length - index - 2);
            
        }
        catch
        {
            return;
        }
    }
}